﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pku.BookStore.Web.Controllers
{
    public class CategoryController : Controller
    {
        //
        // GET: /Category/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(string ID, int? p)
        {
            IServices.ICategoryServices categoryDal = new Services.CategoryServices();
            IServices.IProductServices productDal = new Services.ProductServices();
            if (p == null) p = 1;
            int CateID = 0, CateIDBig = 0, Level = 0;
            if (ID.Contains("_"))
            {
                var IDArr = ID.Split('_');
                CateIDBig = Convert.ToInt32(IDArr[0]);
                if (string.IsNullOrEmpty(IDArr[1]))
                {
                    Level = 1;
                }
                else
                {
                    Level = 2;
                    CateID = Convert.ToInt32(IDArr[1]);
                }
            }
            else
            {
                Level = 1;
                CateIDBig = Convert.ToInt32(ID);
            }
            #region 取同级分类
            var catesameList = categoryDal.GetLevelSame(Level, CateIDBig);
            #endregion

            #region 新书速递
            Model.ProductParam productParam = new Model.ProductParam();
            productParam.PageNo = 1;
            productParam.PageRow = 8;
            productParam.isSpecial = 0;
            var productListNew = productDal.Search(productParam);
            #endregion

            #region 列表
            productParam.PageNo = Convert.ToInt32(p);
            productParam.PageRow = 10;
            productParam.isSpecial = -1;
            productParam.CategoryIDBig = CateIDBig;
            productParam.CategoryID = CateID;
            var productList = productDal.Search(productParam);
            ViewBag.productCount = productDal.SearchCount(productParam);
            #endregion

            IDictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("catesameList", catesameList);
            dic.Add("productListNew", productListNew);
            dic.Add("productList", productList);

            return View(dic);
        }
    }
}
